<?php
class CourseAction extends Action {
    public function index(){
        init($this);
        $this->MenuSelected="Course";
        $model = new Model();
        $res = $model->table("subject")
                     ->join("`select` on select.SubId=subject.SubId")
                     ->join("user on user.UserId=select.UserId")
                     ->join("problem on problem.SubId=subject.SubId")
                     ->field("subject.SubId, subject.SubName, user.UserName, max(problem.ProDate)")
                     ->where("user.UserLevel=2 or user.UserLevel is NULL")
                     ->group("subject.SubId")
                     ->select();
        $this->CourseList = $res;
        $this->display();
    }
    public function detail() {
        $select=$this->_get('cid');
        if (isset($select)) {
            init($this);
            $this->MenuSelected = 'Course';
            // to be continued;
            $this->display();
        }
        else {
            gohistory();
        }
    }
    public function addcourse(){
        init($this);
        if ($this->UserLevel != 3) {
            gohistory();
        }
        else {
            $this->MenuSelected='AddCourse';
            $model = new Model();
            $res = $model->table("user")
                         ->field("user.UserId, user.UserName")
                         ->where("user.UserLevel=2")
                         ->order("user.UserName desc")
                         ->select();
            $this->teacherlist = $res;
            $data['SubName'] = $this->_post('subname');
            $data['UserId'] = $this->_post('teacher');
            $this->error = "&nbsp";
            if (isset($data['SubName']) && isset($data['UserId'])) {
                // addition
                $subject = D('Subject');
                $tmp['SubName'] = $data['SubName'];
                $subject->create($tmp);
                $tmp2['SubId'] = $subject->add();
                $tmp2['UserId'] = $data['UserId'];
                $select = D('Select');
                $select->create($tmp2);
                $select->add();
                $this->error = $subject->getError()."&nbsp".$select->getError();
                if (!$this->error) {
                    $this->error = "添加成功";
                }
            }
            $this->display();
        }
    }
}
?>
